﻿using System.Drawing;
using OfficeOpenXml.Style;
using OfficeOpenXml;

namespace ConsoleSample.Tools.Exports
{
    internal static class EpPlusHelper
    {
        #region AddCellBorder

        public static void AddCellBorder(
            ExcelWorksheet worksheet,
            int rowFrom,
            int columnFrom,
            int rowTo,
            int columnTo
        )
        {
            using var range = worksheet.Cells[rowFrom, columnFrom, rowTo, columnTo];
            range.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.Black);
        }

        public static void AddCellBottomBorder(
            ExcelWorksheet worksheet,
            int rowFrom,
            int columnFrom,
            int rowTo,
            int columnTo
        )
        {
            using var range = worksheet.Cells[rowFrom, columnFrom, rowTo, columnTo];
            range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
            range.Style.Border.Bottom.Color.SetColor(Color.Black);
        }

        public static void AddCellRightBorder(
            ExcelWorksheet worksheet,
            int rowFrom,
            int columnFrom,
            int rowTo,
            int columnTo
        )
        {
            using var range = worksheet.Cells[rowFrom, columnFrom, rowTo, columnTo];
            range.Style.Border.Right.Style = ExcelBorderStyle.Thin;
            range.Style.Border.Right.Color.SetColor(Color.Black);
        }

        public static void AddCellRightBorder(
            ExcelWorksheet worksheet,
            int rowFrom,
            int columnFrom,
            int rowTo,
            int columnTo,
            ExcelBorderStyle style
        )
        {
            using var range = worksheet.Cells[rowFrom, columnFrom, rowTo, columnTo];
            range.Style.Border.Right.Style = style;
            range.Style.Border.Right.Color.SetColor(Color.Black);
        }

        #endregion

        public static void SetCellValue(
            ExcelWorksheet worksheet,
            Font font,
            int row,
            int column,
            string value
        )
        {
            using var range = worksheet.Cells[row, column];
            range.Value = value;
            range.Style.Font.SetFromFont(font);
            range.Style.Font.Color.SetColor(Color.Black);
            range.Style.HorizontalAlignment = ExcelHorizontalAlignment.CenterContinuous;
            range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            range.Style.Fill.PatternType = ExcelFillStyle.Solid;
            range.Style.Fill.BackgroundColor.SetColor(Color.White);
        }

        public static void SetCellValueWithoutFill(
            ExcelWorksheet worksheet,
            Font font,
            int row,
            int column,
            string? value,
            ExcelHorizontalAlignment horizontalAlignment = ExcelHorizontalAlignment.CenterContinuous,
            ExcelVerticalAlignment verticalAlignment = ExcelVerticalAlignment.Center,
            bool wrapText = false
        )
        {
            using var range = worksheet.Cells[row, column];
            range.Value = value;
            range.Style.Font.SetFromFont(font);
            range.Style.Font.Color.SetColor(Color.Black);
            range.Style.HorizontalAlignment = horizontalAlignment;
            range.Style.VerticalAlignment = verticalAlignment;
            range.Style.WrapText = wrapText;
        }

        //合并单元格
        public static void MergeCell(
            ExcelWorksheet worksheet,
            int rowFrom,
            int rowTo,
            int columnFrom,
            int columnTo
        )
        {
            var range = GetRange(rowFrom, rowTo, columnFrom, columnTo);
            using var r = worksheet.Cells[range];
            r.Merge = true;
        }

        public static void EnableWrapText(
            ExcelWorksheet worksheet,
            int rowFrom,
            int rowTo,
            int columnFrom,
            int columnTo
        )
        {
            var range = GetRange(rowFrom, rowTo, columnFrom, columnTo);
            using var r = worksheet.Cells[range];
            r.Style.WrapText = true;
        }

        public static string GetRange(
            int rowFrom,
            int rowTo,
            int columnFrom,
            int columnTo
        )
        {
            var columnFromChar = (char)('A' + columnFrom - 1);
            var columnToChar = (char)('A' + columnTo - 1);
            return $@"{columnFromChar}{rowFrom}:{columnToChar}{rowTo}"; //A2:G2
        }
    }
}
